#include "semi/util/SocketUtil.h"
#include "TASK01_Controller.h"

using namespace semi;
using namespace semi::util;

void receiveUDP() {

	bool bl = true;
	SocketUtil su(1300);
	//	std::cout << "udpBind" << std::endl;
	bl = su.udpBind();
	if (!bl)
		std::cout << "udpBind false" << std::endl;

	//	while (bl) {
	int siz = su.udpReceive();

	std::cout << " udpReceive " << std::string((char*) su.message, siz)
			<< std::endl;
	//		sleep(1);
	//	}

	su.end();
}

void sendUDP() {

	bool bl;
	//	SocketUtil su("localhost", 1300);
	SocketUtil su("127.0.0.1", 1300);
	//	bl = su.udpBind();
	if (bl)
		bl = su.udpSend("message test");
	if (!bl)
		std::cout << "udpSend false" << std::endl;
}

void receive() {

	SocketUtil su(1300);
	std::cout << "tcpListen" << std::endl;
	bool bl = su.tcpListen(3);

	if (!bl)
		std::cout << "tcpListen false" << std::endl;

	//	while (bl) {
	int ret = su.tcpReceive(sizeof(int), sizeof(int));

	std::cout << " tcpReceive ret:" << ret << " first:" << std::string(
			(char*) su.message, 3) << " last:" << std::string(
			(char*) su.message + ret - 3, 3) << " " << std::string(
			(char*) su.message, ret) << std::endl;
	//	sleep(1);
	//}

	su.end();
}

void send() {

	string
			msg =
					string(
							"message test0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789");

	void* tmp = malloc(sizeof(int) + msg.length());
	int size = msg.length();
	memcpy(tmp, &size, sizeof(int));

	memcpy((char*) tmp + sizeof(int), msg.c_str(), msg.length());
	memset((char*) tmp + 20, 0, 1); // null

	//	SocketUtil su("localhost", 1300);
	SocketUtil su("127.0.0.1", 1300);
	bool bl = su.tcpConnect(5);
	if (bl)
		bl = su.tcpSend(tmp, (sizeof(int) + msg.length()), 5);
	//		bl = su.tcpSend(msg, 5);

	if (!bl)
		std::cout << "tcpSend false" << std::endl;
	else
		std::cout << "tcpSend true" << std::endl;

}

void message_receive() {

}

void message_send() {

}

int main() {

	conf::Configurator::setIninitial();

	// receive();
	// send();

	//	receiveUDP();
	//	sendUDP();

	//	TASK01_Controller().doProcess("TASK01"); // 5432
	// ipc::MessageQue("TASK01").send("dummy");
	//	ipc::MessageQue("TASK01").sweep();

	//	CurrentTasks("TASK01").setValid(false);
	//	conf::NetConfig::TcpConfig cnf = conf::NetConfig().getTcpConfig("TASK01");
	//	NetMessage::sweep(cnf);

	//	SocketUtil su("127.0.0.1", 5432);
	//	bool bl = su.tcpConnect(5);
	//	if (bl)
	//		bl = su.tcpSend("message 1234567890123456789", 5);
	//	su.end();

	//	CurrentTasks("TASK02").setValid(false);
	//	conf::NetConfig::TcpConfig cnf = conf::NetConfig().getConfig("TASK02");
	//	NetMessage::sweep( cnf);
	//	SocketUtil su("127.0.0.1", 5433);
	//	bool bl = su.tcpConnect(5);
	//	if (bl)
	//		bl = su.tcpSend("message 1234567890123456789", 5);


	//	logg::Logger::releaseLockAll();
	//	conf::NetConfig().getConfig("TASK01");

	//	conf::TasksConfig().getConfig("TASK01");
	//	cout << conf::TasksConfig::toString() << endl;
	// std::cout << CurrentTasks::toString() << std::endl;

	std::cout << conf::NetConfig::toString() << std::endl;

	return 0;
}
